#ifndef _CONFIG_H_
#define _CONFIG_H_

#include <stdio.h>
#include <unistd.h>
#include <fcntl.h>
#include "strfix.h"


#define DEFAULT_IP      "127.0.0.1"
#define DEFAULT_PORT    9000

int get_conf(char *ip, int *port)
{
    int fd;
    char buf[1024] = {0};
    char file[1024] = {0};
    char port_buf[64] = {0};
    char *p = buf;

    char *home = getenv("HOME");
    sprintf(file, "%s/.config/ffshare.conf", home);

    fd = open(file, O_RDONLY);
    if (fd < 0) {
        strcpy(ip, DEFAULT_IP);
        *port = DEFAULT_PORT;
        return -1;
    }

    read(fd, buf, sizeof(buf));

    close(fd);

    p = strbtwn(p, "ip=", "\n", ip);
    p = strbtwn(p, "port=", "\n", port_buf);
    strint(port_buf, port);
    return 0;
}

#endif
